# gozerlib/utils/web.py
#
#

""" web related functions. """

## gozerlib imports

from gozerlib.utils.generic import fromenc, getversion
from gozerlib.utils.auth import finduser

## google imports

from google.appengine.api import users as gusers
from google.appengine.ext.webapp import template
from google.appengine.api.memcache import get_stats

## basic imports

import os
import time

def start(response, input={}):

    """ display start html so that bot output can follow. """

    inputdict = {'version': getversion(), 'who': finduser()}
    if input:
        inputdict.update(input)
    temp = os.path.join(os.getcwd(), 'templates/start.html')
    outstr = template.render(temp, inputdict)
    response.out.write(outstr)

def commandbox(response, url="/dispatch/"):

    """ write html data for the exec box. """

    response.out.write("""
          <form action="%s" method="post">
            <div><b>enter command:</b> <input type="commit" name="content"></div>
          </form>
          """ % url)

def execbox(response, url="/exec/"):

    """ write html data for the exec box. """

    response.out.write("""
      <form action="" method="GET">
        <b>enter command:</b><input type="commit" name="input" value="">
        // <input type="button" value="go" onClick="makePOSTRequest(this.form)"
      </form>
          """)

def closer(response):

    """ send closing html .. comes after the bot output. """

    stats = get_stats()

    response.out.write('<div class="footer">')
    response.out.write('<b>%4f seconds</b> | cache: %s</div>' % (time.time() - response.starttime, str(stats)))
    response.out.write('</body></html>')

def loginurl(response):

    """ show google login url. """

    response.out.write('<br><br>')
    response.out.write('<div class="body">')
    plzlogin = ("you are not logged in .. <a href=\"%s\">login</a>." %
    gusers.create_login_url("/"))
    response.out.write(plzlogin)
    response.out.write("</div>")
